<template>
  <div class="container">
    <div class="top">
      <el-input
        style="width:200px;margin-bottom:5px"
        v-model="search.searchCode"
        placeholder="请输入分类名称或编码"
        clearable
      ></el-input>
      <el-button
        size="small"
        type="primary"
        class="handleSearch"
        icon="el-icon-search"
        @click="handleSearch()"
      >查询</el-button>
    </div>
    <el-table max-height="600"
      :data="tableData"
      border
      style="width: 100%"
      empty-text="暂无数据"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="50" align="center"></el-table-column>
      <el-table-column prop="name" label="erp分类名称" width="180" align="center"></el-table-column>
      <el-table-column prop="code" label="erp分类编号" width="180" align="center"></el-table-column>
      <el-table-column prop="isEnd" label="是否末级" width="80" align="center"></el-table-column>
       <el-table-column label="分类" width="150" align="center">
        <template slot-scope="scope">
          <el-input  v-model="scope.row.class_name" size="mini"  @change="aliasName(scope)" placeholder="请输入分类名"></el-input>
        </template>
      </el-table-column>
      <el-table-column label="税率" width="180" align="center">
        <template slot-scope="scope">
          <el-input-number
            :controls="false"
            v-model="scope.row.class_tax_rate"
            size="mini"
            :min="0"
            controls-position="right"
            :precision="2"
            :step="1"
            @change="taxrate(scope)"
          ></el-input-number>
        </template>
      </el-table-column>
      <el-table-column prop="create_at" label="添加时间" align="center"></el-table-column>
      <el-table-column prop="update_at" label="最后同步时间" align="center"></el-table-column>
     
    </el-table>
    <!-- 分页 -->
    <pagination @page="handleSizeChange" @pagesize="handleCurrentChange" :pageData="pageData"></pagination>
  </div>
</template>
<script>
import { parseTime } from "@/utils/index";
import Pagination from "@/components/pagination";
import { classifyList,setClassName,setClassRate } from "@/api/commodity/commodityList";
export default {
  components: {
    pagination: Pagination
  },
  data() {
    return {
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper"
      },
      search: {
        searchCode: ""
      }
    };
  },

  created() {
    //页面数据
    this.getData();
  },
  methods: {
    //页面数据
    getData() {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        search: this.search.searchCode
      };
      classifyList(data)
        .then(res => {
          if (res.code == 0) {
            res.data.data.forEach(res => {
              res.isEnd = res.is_end == 1 ? "是" : "不是";
              res.create_at = parseTime(res.add_time);
              res.update_at = parseTime(res.update_time);
            });
            this.tableData = res.data.data;
            this.pageData.page = res.data.current_page;
            this.pageData.pagesize = res.data.per_page;
            this.pageData.total = res.data.total;
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
     //修改分类名
    aliasName(scope){
       let data = {
        erp_class_id: scope.row.productclass_id,
        name: scope.row.class_name
      };
      console.log(scope)
      setClassName(data).then(res => {
        console.log(res);
        if (res.code == 0) {
          this.$message({
            message: res.msg,
            type: "success"
          });
        }
      });
    },
      //修改税率
    taxrate(scope){
      let data = {
        erp_class_id: scope.row.productclass_id,
        rate: scope.row.class_tax_rate
      };
      setClassRate(data).then(res => {
        console.log(res);
        if (res.code == 0) {
          this.$message({
            message: res.msg,
            type: "success"
          });
        }
      });
    },
    // 本地搜索
    handleSearch() {
      this.pageData.page = 1;
      this.pageData.total = null;
      this.getData();
    },

    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;

      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;

      this.getData();
    },
    //选取
    handleSelectionChange(val) {
      this.multipleSelection = val;
    }
  }
};
</script>
<style scoped>
.top {
  position: relative;
}

.add {
  position: absolute;
  right: 0;
  top: 4px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>